import { defineStore } from 'pinia'
import { createDiscreteApi, NIcon } from 'naive-ui'
import type { Component } from 'vue'
import type { ConfigProviderProps } from 'naive-ui'
import type { DialogOptions } from 'naive-ui/es/dialog/src/DialogProvider'

/** n-config-provider 配置 */
export const useNaive = defineStore('naive', {
	state() {
		return {
			config: {}
		} as {
			/** naive 全局配置 */
			config: CanWrite<ConfigProviderProps>
		}
	}
})

/** 将 Icon 组件渲染成 Nicon */
export const renderIcon = (icon: Component) => () => h(NIcon, null, { default: () => h(icon) })

export type CanCelMethod = 'onEsc' | 'onClose' | 'onMaskClick' | 'onNegativeClick'
type CustomDialogOpts = Omit<DialogOptions, CanCelMethod | 'onPositiveClick'>
type HasTypeCustomDialogOpts = Omit<DialogOptions, 'type' | CanCelMethod>

const apis = createDiscreteApi(['dialog', 'loadingBar', 'message', 'notification'])
const $dialog = (opts: CustomDialogOpts) =>
	new Promise((resolve, reject: (err: CanCelMethod) => any) => {
		apis.dialog.create({
			...opts,
			onPositiveClick: () => resolve(undefined),
			onEsc: () => reject('onEsc'),
			onClose: () => reject('onClose'),
			onMaskClick: () => reject('onMaskClick'),
			onNegativeClick: () => reject('onNegativeClick')
		})
	})
$dialog.destroyAll = apis.dialog.destroyAll
$dialog.info = (opts: HasTypeCustomDialogOpts) => $dialog({ type: 'info', ...opts })
$dialog.error = (opts: HasTypeCustomDialogOpts) => $dialog({ type: 'error', ...opts })
$dialog.default = (opts: HasTypeCustomDialogOpts) => $dialog({ type: 'default', ...opts })
$dialog.success = (opts: HasTypeCustomDialogOpts) => $dialog({ type: 'success', ...opts })
$dialog.warning = (opts: HasTypeCustomDialogOpts) => $dialog({ type: 'warning', ...opts })
/**
 * @link https://www.naiveui.com/zh-CN/os-theme/components/dialog
 * @example
 * import { dialog } from '@/store/useNaive'
 * import type { CanCelMethod } from '@/store/useNaive' // 导入 catch 的错误类型
 *
 * dialog({
 * 	title: '标题',
 * 	content: '内容'.repeat(50),
 * 	positiveText: '确认',	// 不填不显示确认按钮
 * 	negativeText: '取消'	// 不填不显示取消按钮
 * 	// ……
 * })
 * 	.then(() => {
 * 		// 做你想做……
 * 	})
 * 	.catch((err: CanCelMethod) => {
 * 		switch(err) {
 * 			case 'onEsc':
 * 			case 'onClose':
 * 			// …………
 * 		}
 * 	})
 */
export const dialog = $dialog

/**
 * @link https://www.naiveui.com/zh-CN/os-theme/components/loading-bar
 * @example
 * import { loadingBar } from '@/store/useNaive'
 * loadingBar.start()
 * setTimeout(Math.random() > 0.5 ? loadingBar.finish : loadingBar.error, 5000)
 */
export const loadingBar = apis.loadingBar

/**
 * @link https://www.naiveui.com/zh-CN/os-theme/components/message
 * @example
 * import { message } from '@/store/useNaive'
 * message.success('hello, word!')
 * const loadingMsg = message.loading('Are you OK?')
 * setTimeout(loadingMsg.destroy, 5000)
 */
export const message = apis.message

/**
 * @link https://www.naiveui.com/zh-CN/os-theme/components/notification
 * @example
 * import { NAvatar } from 'naive-ui'
 * notification.create({
 * 	title: '标题',
 * 	content: '内容'.repeat(50),
 * 	description: '描述'.repeat(5),
 * 	meta: '2022-11-02',
 * 	avatar: () => h(
 * 		NAvatar,
 * 		{
 * 			size: 'small',
 * 			round: true,
 * 			src: 'https://wuhaochao.top/static/users/2/nas/file/funny.png'
 * 		}
 * 	)
 * })
 */
export const notification = apis.notification
